package com.project.service.impl;

import com.project.bean.ToolBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IToolMapper;
import com.project.service.IToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ToolServiceImpl implements IToolService {

    @Autowired
    private IToolMapper mapper;

    @Autowired
    private IRoomMapper roomMapper;

    @Override
    public List<ToolBean> findById(Integer roomId) {
        return mapper.findById(roomId);
    }

    @Override
    public void addToolRecord(ToolBean tool) {
        mapper.addToolRecord(tool);
        roomMapper.updateStatusById(tool.getRoomBean().getRoomId(), "设施损坏");
    }

    @Override
    public void updateToolRecord(Integer toolId, Integer roomId) {
        //按设施的id修改解决状态为“已解决”。
        mapper.updateToolRecord(toolId);
        //按房间id统计状态为“未解决”设施数量。
        if (mapper.findRoomUnsolvedNumById(roomId) == 0) {
            //如果为0，则将房间状态修改为“正常”
            roomMapper.updateStatusById(roomId, "正常");
        }

    }
}
